package com.csw.shuanfa.CodeImprove.redlockThread.locktest.service;

import com.csw.shuanfa.CodeImprove.redlockThread.locktest.service.impl.DateBaseComponentImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SynchronizedService {
    @Autowired
    private DateBaseComponentImpl dateBaseComponentImpl;

    private final Object object = new Object();

    //     ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();
//     ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,
//            new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
    public void oversold(int reqNums) {
        try {
            for (int i = 0; i < reqNums; i++) {
                // 一个线程模拟一个请求
                new Thread(() -> {
                    synchronized (object) {//synchronized 只能锁本服务的资源；多台服务的资源是锁不住的
                        dateBaseComponentImpl.synchronizedDecrment();
                    }
                }).start();
            }
        } catch (Exception e) {

        }
    }


}
